package com.tengju.user.domain.model.login;

import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.DomainExceptionCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Value
public class LoginForm {

    /**
     * 区号
     */
    @NonFinal
    private String areaCode;

    /**
     * 用户名
     */
    @NonFinal
    private String username;

    /**
     * 密码
     */
    @NonFinal
    private String password;

    /**
     * 短信验证码
     */
    @NonFinal
    private String verificationCode;

    /**
     * 用户类型
     */
    private UserType userType;

    /**
     * 登陆类型
     */
    private LoginType loginType;

    /**
     * 登陆ip
     */
    private String loginIp;

    /**
     * 登陆时间
     */
    private LocalDateTime loginTime;

    private String from;

    /**
     * 微信小程序授权code码
     */
    @NonFinal
    private String authCode;

    public LoginForm(String areaCode, String username, String password, String verificationCode, UserType userType, LoginType loginType, String loginIp, LocalDateTime loginTime, String from) {

        if (StringUtils.isEmpty(username)) {
            throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "手机号或用户名为空");
        }
        if (userType == null) {
            throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "用户类型为空");
        }
        if (loginType == null) {
            throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "登陆类型为空");
        }
        if (loginType == LoginType.PWD_LOGIN && StringUtils.isEmpty(password)) {
            throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "密码为空");
        }
        if (loginType == LoginType.MOBILE_CODE_LOGIN) {
            if (StringUtils.isEmpty(verificationCode)) {
                throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "手机验证码为空");
            }
            if (StringUtils.isEmpty(areaCode)) {
                throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "区号为空");
            }
        }
        this.areaCode = areaCode;
        this.username = username;
        this.password = password;
        this.verificationCode = verificationCode;
        this.userType = userType;
        this.loginType = loginType;
        this.loginIp = loginIp;
        this.loginTime = loginTime;
        this.from = from;
    }

    public LoginForm(UserType userType, LoginType loginType, String loginIp, LocalDateTime loginTime, String from, String authCode) {
        if (loginType == LoginType.QY_WECHAT_MINI_PROGRAM_LOGIN && StringUtils.isEmpty(authCode)) {
            throw DomainException.error(DomainExceptionCode.SYS_PARAMS_NULL, "微信授权码不能为空");
        }
        this.authCode = authCode;
        this.userType = userType;
        this.loginType = loginType;
        this.loginIp = loginIp;
        this.loginTime = loginTime;
        this.from = from;
    }


    public String username() {
        if (userType == UserType.USER) {
            return areaCode + username;
        } else {
            return username;
        }
    }


}
